package com.servlet.request; /**
 * @author wzm
 * @create 2023-11-06 11:26
 * project_name:web2310
 * @Description
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

@WebServlet("/req5")
public class RequestServlet5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);//调用doPost
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //测试Get请求和Post请求都通用的获取数据的方式
        //1.获取所有参数的集合 Map
       /* Map<String, String[]> map = request.getParameterMap();
        //遍历Map集合中各个属性的值
        //遍历map集合key
        for (String key : map.keySet()) {
            //输出key username password hobby
            System.out.println("key:" + key + " ");

            //获取值 根据key获取值
            String[] strings = map.get(key);
            for (String value : strings) {
                System.out.println("value:" + value + " ");
            }

        }
*/

        //2.根据名称获取参数值的数组 使用的是input的name属性
        String[] values = request.getParameterValues("hobby");
        for (String s : values) {
            System.out.println("s = " + s);
        }

        //3.根据名称获取单个参数值
        String username = request.getParameter("username");
        System.out.println("username = " + username);
        String password = request.getParameter("password");
        System.out.println("password = " + password);

    }
}
